package com.my.zxsyd.controller.admin;


import cn.dev33.satoken.stp.StpUtil;
import com.my.zxsyd.entity.Admin;
import com.my.zxsyd.service.IAdminService;
import com.my.zxsyd.util.ResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author lbk
 * @since 2024-02-19
 */
@RestController
@RequestMapping("/api/admin/admin")
public class AdminController {

    @Autowired
    private IAdminService iAdminService;

    /**
     * 管理员登录
     * @param account
     * @param pwd
     * @return
     */
    @RequestMapping("/login")
    public ResultView login(String account,String pwd){
        if(StringUtils.isEmpty(account)){
            return ResultView.error("账号不能为空");
        }
        if(StringUtils.isEmpty(pwd)){
            return ResultView.error("密码不能为空");
        }
        Admin admin=this.iAdminService.login(account,pwd);
        if(admin==null){
            return ResultView.error("账号密码错误");
        }
        StpUtil.login(admin.getId());
        return ResultView.ok(StpUtil.getTokenValue());
    }

    /**
     * 获取当前登录的用户信息
     * @return
     */
    @RequestMapping("/getAccountInfo")
    public ResultView getAccountInfo(){
        String id=StpUtil.getLoginIdAsString();
        Admin admin=this.iAdminService.getById(id);
        return ResultView.ok(admin);
    }


}
